home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1103 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: aril@cmt.lpr.mail.carel.fi (Ari Lukumies)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to dynamically allocate first element of linked list.
  5. Date: Thu, 11 Jan 1996 13:22:31 GMT
  6. Organization: Carelcomp Forest Oy
  7. Message-ID: <4d33b7$kg3@tahko.lpr.carel.fi>
  8. References: <erkDL007G.rt@netcom.com>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. erk@netcom.com (Staugher) wrote:
  13.  
  14. >Im trying (without any luck )to write a function to allocate the first element 
  15. >of a doubly linked list in C++. Unfortunately I learned data structures in 
  16. >Pascal and have to port my knowledge (or lack of ) to C.
  17.  
  18. >Here is my arrangement:
  19.  
  20. [stuff deleted]
  21.  
  22. >struct elmnt * newlist(void)
  23. >{
  24. >struct elmnt *newptr;
  25. >newptr = malloc(sizeof(struct elmnt));
  26. >return (newptr);
  27. >}
  28.  
  29. >//=======================================
  30.  
  31. >The compiler complains with an ERROR that it cannot convert
  32. >(void *) to (elmnt *) in function newlist
  33.  
  34. >I hope that the problem is obvious to someone :/
  35.  
  36. >ADV thanks ANCE
  37.  
  38. C++ uses strong type checking, which is why your code won't compile
  39. (malloc returns a void*, whereas newptr wants struct elmnt*.
  40. Correction is to write:
  41.  
  42.     newptr = (struct elmnt *)malloc(sizeof(struct elmnt));
  43.  
  44. Later,
  45. AriL
  46.  
  47. All my opinions are mine and mine alone.
  48.  
  49.